%23%20%2F%2F%2F%20script%0A%23%20requires-python%20%3D%20%22%3E%3D3.13%22%0A%23%20dependencies%20%3D%20%5B%0A%23%20%20%20%20%20%22kaleido%3D%3D1.2.0%22%2C%0A%23%20%20%20%20%20%22marimo%22%2C%0A%23%20%20%20%20%20%22numpy%3D%3D2.4.2%22%2C%0A%23%20%20%20%20%20%22pandas%3D%3D3.0.0%22%2C%0A%23%20%20%20%20%20%22plotly%3D%3D6.5.2%22%2C%0A%23%20%20%20%20%20%22scipy%3D%3D1.17.0%22%2C%0A%23%20%20%20%20%20%22statsmodels%3D%3D0.14.6%22%2C%0A%23%20%5D%0A%23%20%2F%2F%2F%0A%0Aimport%20marimo%0A%0A__generated_with%20%3D%20%220.19.7%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.hstack(%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.image(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20src%3D%22http%3A%2F%2Fmodernmacro.org%2Fresources%2FVivaldo%2Ffigures%2FOldTests%2FRIR.png%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20width%3D200%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.image(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20src%3D%22http%3A%2F%2Fmodernmacro.org%2Fresources%2FVivaldo%2Ffigures%2Fiscte.png%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20width%3D300%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20align%3D%22center%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%3Cstyle%3E%0A%20%20%20%20.marimo-cell%20%7B%0A%20%20%20%20%20%20%20%20border%3A%20none%20!important%3B%0A%20%20%20%20%20%20%20%20box-shadow%3A%20none%20!important%3B%0A%20%20%20%20%20%20%20%20margin%3A%200%20!important%3B%0A%20%20%20%20%20%20%20%20padding%3A%200%20!important%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20.marimo-cell-content%20%7B%0A%20%20%20%20%20%20%20%20padding%3A%200.5rem%200%20!important%3B%0A%20%20%20%20%7D%0A%20%20%20%20.admonition%20%7B%0A%20%20%20%20margin-top%3A%20-0.99rem%20!important%3B%0A%20%20%20%20margin-bottom%3A%20-0.99rem%20!important%3B%0A%20%20%20%20%7D%0A%20%20%20%20h1%20%7B%0A%20%20%20%20%20%20%20%20margin-bottom%3A%20-1.2rem%20!important%3B%0A%20%20%20%20%7D%0A%20%20%20%20h2%20%7B%0A%20%20%20%20%20%20%20%20margin-bottom%3A%20-1.2rem%20!important%3B%0A%20%20%20%20%7D%0A%20%20%20%20%2F*%20Reduce%20spacing%20before%20bullet%20lists%20*%2F%0A%20%20%20%20ul%20%7B%0A%20%20%20%20%20%20%20%20margin-top%3A%20-0.3rem%20!important%3B%0A%20%20%20%20%7D%0A%20%20%20%20%2F*%20Optionally%20reduce%20spacing%20between%20list%20items%20*%2F%0A%20%20%20%20li%20%7B%0A%20%20%20%20%20%20%20%20margin-bottom%3A%200.2rem%20!important%3B%0A%20%20%20%20%7D%0A%20%20%20%20%2F*%20Increase%20font%20size%20for%20all%20text%20in%20the%20document%20*%2F%0A%20%20%20%20body%20%7B%0A%20%20%20%20%20%20%20%20font-size%3A%2016px%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20%2F*%20Increase%20font%20size%20for%20all%20headers%20*%2F%0A%20%20%20%20h1%20%7B%0A%20%20%20%20%20%20%20%20font-size%3A%2036px%20!important%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20h2%20%7B%0A%20%20%20%20%20%20%20%20font-size%3A%2028px%20!important%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20h3%20%7B%0A%20%20%20%20%20%20%20%20font-size%3A%2025px%20!important%3B%0A%20%20%20%20%7D%0A%20%20%20%20%3C%2Fstyle%3E%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Recap%3A%20Basic%20Statistics%20%26%20Dynamic%20Processes%0A%0A%20%20%20%20%3Cbr%3E%0A%20%20%20%20**02%20February%202026**%0A%20%20%20%20%3Cbr%3E%0A%0A%20%20%20%20**Vivaldo%20Mendes**%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20---%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Packages%20used%20in%20this%20notebook%0A%0A%20%20%20%20%7C%20Package%20%7C%20Role%20%7C%0A%20%20%20%20%7C---%7C---%7C%0A%20%20%20%20%7C%20%60marimo%60%20%7C%20Reactive%20notebook%20framework%20%7C%0A%20%20%20%20%7C%20%60numpy%60%20%7C%20Numerical%20computing%20%26%20linear%20algebra%20%7C%0A%20%20%20%20%7C%20%60pandas%60%20%7C%20Data%20manipulation%20%26%20CSV%20I%2FO%20%7C%0A%20%20%20%20%7C%20%60plotly%60%20%7C%20Interactive%20plotting%20%7C%0A%20%20%20%20%7C%20%60statsmodels%60%20%7C%20%60acf%60%20%2F%20%60ccf%60%20(autocorrelation%20%26%20cross-correlation)%20%7C%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20pandas%20as%20pd%0A%20%20%20%20from%20scipy.optimize%20import%20fsolve%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20import%20plotly.graph_objects%20as%20go%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20from%20plotly.subplots%20import%20make_subplots%0A%20%20%20%20import%20plotly.express%20as%20px%0A%20%20%20%20import%20plotly.io%20as%20pio%20%0A%20%20%20%20pio.templates.default%20%3D%20%22plotly%22%0A%20%20%20%20import%20kaleido%0A%20%20%20%20pio.defaults.default_format%20%3D%20%22svg%22%0A%20%20%20%20from%20datetime%20import%20date%2C%20datetime%0A%20%20%20%20from%20numpy.linalg%20import%20eigvals%2C%20inv%0A%20%20%20%20from%20statsmodels.tsa.stattools%20import%20acf%2C%20ccf%20as%20statsmodels_ccf%0A%20%20%20%20import%20warnings%0A%20%20%20%20warnings.filterwarnings(%22ignore%22)%0A%20%20%20%20import%20os%0A%20%20%20%20os.chdir(os.path.dirname(__file__))%0A%20%20%20%20return%20acf%2C%20eigvals%2C%20go%2C%20inv%2C%20mo%2C%20np%2C%20pd%2C%20px%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%201.%20Basic%20statistical%20concepts%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20This%20is%20how%20most%20economic%20data%20looks%20like%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(go%2C%20inflation)%3A%0A%20%20%20%20fig_portugal%20%3D%20go.Figure(%0A%20%20%20%20%20%20%20%20data%3D%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20x%3Dinflation%5B%22Months%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20y%3Dinflation%5B%22Portugal%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22markers%2Blines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(symbol%3D%22circle%22%2C%20size%3D5%2C%20color%3D%22darkred%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(width%3D0.3)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20)%0A%20%20%20%20fig_portugal.update_layout(%0A%20%20%20%20%20%20%20%20title_text%3D%22Inflation%20in%20Portugal%22%2C%0A%20%20%20%20%20%20%20%20title_x%3D0.5%2C%0A%20%20%20%20%20%20%20%20height%3D450%2C%0A%20%20%20%20%20%20%20%20hovermode%3D%22x%22%2C%0A%20%20%20%20%20%20%20%20xaxis_range%3D%5B%221996-08-01%22%2C%20%222024-06-01%22%5D%2C%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D70%2C%20r%3D60%2C%20t%3D70%2C%20b%3D60)%0A%20%20%20%20)%0A%20%20%20%20fig_portugal%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(inflation%2C%20px)%3A%0A%20%20%20%20%23%20Bar%20plot%20of%20inflation%20deviation%20from%20target%0A%20%20%20%20fig8%20%3D%20px.bar(x%3Dinflation.Months%2C%20y%3D(inflation.EuroArea%20-%202)%2C%20color_discrete_sequence%3D%5B'darkred'%5D%20)%0A%20%20%20%20fig8.update_layout(height%3D450%2C%20title_text%3D'EuroArea%20Inflation%20Deviation%20from%202%25%20Target'%2C%20hovermode%20%3D%20%22x%22%2C%20margin%3Ddict(l%3D70%2C%20r%3D60%2C%20t%3D70%2C%20b%3D60))%0A%20%20%20%20fig8%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20This%20is%20how%20random%20shocks%20look%20like%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(go%2C%20np)%3A%0A%20%20%20%20np.random.seed(42)%0A%20%20%20%20random_obs%20%3D%20np.random.randn(300)%0A%0A%20%20%20%20fig_random%20%3D%20go.Figure(%0A%20%20%20%20%20%20%20%20data%3D%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20y%3Drandom_obs%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22markers%2Blines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(symbol%3D%22circle%22%2C%20size%3D5%2C%20color%3D%22darkred%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(width%3D0.3)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20)%0A%20%20%20%20fig_random.update_layout(%0A%20%20%20%20%20%20%20%20height%3D450%2C%0A%20%20%20%20%20%20%20%20title%3D%22Two%20hundred%20random%20normally%20distributed%20observations%22%2C%0A%20%20%20%20%20%20%20%20title_font_size%3D18%2C%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D70%2C%20r%3D60%2C%20t%3D70%2C%20b%3D60)%20%20%0A%20%20%20%20)%0A%20%20%20%20fig_random%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20The%20sample%20mean%0A%0A%20%20%20%20For%20a%20given%20real-valued%20array%20%24X%24%2C%20the%20sample%20*mean*%20is%20given%20by%0A%0A%20%20%20%20%24%24%5Coverline%7Bx%7D%3D%5Cfrac%7Bx_%7B1%7D%2Bx_%7B2%7D%2B%5Cldots%2Bx_%7Bn%7D%7D%7Bn%7D%3D%5Cfrac%7B%5Csum_%7Bi%3D1%7D%5E%7Bn%7D%20x_%7Bi%7D%7D%7Bn%7D%20%5Ctag%7B1%7D%24%24%0A%0A%20%20%20%20where%20%24n%24%20is%20the%20length%20of%20the%20input%2C%20%24x_i%24%20are%20the%20observations%20in%20%24X%24%2C%20and%20%24%5Coverline%7Bx%7D%24%20is%20the%20sample%20mean.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%23%20Sample%20variance%20and%20standard%20deviation%0A%0A%20%20%20%20The%20sample%20*variance*%20and%20the%20sample%20*standard%20deviation*%20are%20given%20by%2C%20respectively%3A%0A%0A%20%20%20%20%24%24s_x%5E%7B2%7D%3D%5Coperatorname%7BVar%7D(x)%3D%5Cfrac%7B%5Csum_%7Bi%3D1%7D%5E%7Bn%7D%5Cleft(x_%7Bi%7D-%5Cbar%7Bx%7D%5Cright)%5E%7B2%7D%7D%7Bn-1%7D%20%5Ctag%7B2%7D%24%24%0A%0A%20%20%20%20and%0A%0A%20%20%20%20%24%24s_x%3D%5Csqrt%7B%5Cfrac%7B%5Csum_%7Bi%3D1%7D%5E%7Bn%7D%5Cleft(x_%7Bi%7D-%5Cbar%7Bx%7D%5Cright)%5E%7B2%7D%7D%7Bn-1%7D%7D%20%5Ctag%7B3%7D%24%24%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20The%20Auto-Correlation%20function%0A%0A%20%20%20%20To%20compute%20the%20covariance%20between%20the%20vectors%20%24x%24%20and%20%24y%24%2C%20we%20can%20use%20the%20Pearson's%20**correlation%20coefficient**.%20When%20applied%20to%20a%20sample%2C%20the%20former%20is%20commonly%20represented%20by%20%24r_%7Bxy%7D%24%20and%20may%20be%20referred%20to%20as%20the%20sample%20correlation%20coefficient.%20We%20can%20obtain%20a%20formula%20for%20%24r_%7Bxy%7D%24%20by%20substituting%20estimates%20of%20the%20covariances%20and%20variances%20based%20on%20a%20sample%20into%20the%20formula%20above.%20Given%20paired%20data%20%24%5Cleft%5C%7B%5Cleft(x_%7B1%7D%2C%20y_%7B1%7D%5Cright)%2C%20%5Cldots%2C%5Cleft(x_%7Bn%7D%2C%20y_%7Bn%7D%5Cright)%5Cright%5C%7D%24%20consisting%20of%20%24n%24%20pairs%2C%20%24r_%7Bxy%7D%24%20is%20defined%20as%3A%0A%0A%20%20%20%20%24%24r_%7Bxy%7D%3D%5Cfrac%7B1%7D%7Bn-1%7D%20%5Csum_%7Bi%3D1%7D%5E%7Bn%7D%5Cleft(x_%7Bi%7D-%5Cbar%7Bx%7D%5Cright)%5Cleft(y_%7Bi%7D-%5Cbar%7By%7D%5Cright)%20%5Ctag%7B4%7D%24%24%0A%0A%20%20%20%20where%3A%0A%0A%20%20%20%20-%20%24n%24%20is%20sample%20size%0A%0A%20%20%20%20-%20%24x_%7Bi%7D%2C%20y_%7Bi%7D%24%20are%20the%20individual%20sample%20points%20indexed%20with%20%24i%24%0A%0A%20%20%20%20-%20%24%5Cbar%7Bx%7D%3D%5Cfrac%7B1%7D%7Bn%7D%20%5Csum_%7Bi%3D1%7D%5E%7Bn%7D%20x_%7Bi%7D%24%20(the%20sample%20mean)%3B%20and%20analogously%20for%20%24%5Cbar%7By%7D%24%0A%0A%20%20%20%20The%20**autocorrelation%20function**%20with%20%24k%24%20lags%20of%20vector%20%24y%24%20is%20defined%20as%0A%0A%20%20%20%20%24%24r_%7By%7D(k)%3D%5Cfrac%7B%5Csum_%7Bi%3D1%7D%5E%7Bn-k%7D%5Cleft(y_%7Bi%7D-%5Cbar%7By%7D%5Cright)%5Cleft(y_%7Bi%2Bk%7D-%5Cbar%7By%7D%5Cright)%7D%7B%5Csum_%7Bi%3D1%7D%5E%7Bn%7D%5Cleft(y_%7Bi%7D-%5Cbar%7By%7D%5Cright)%5E%7B2%7D%7D%20%5Ctag%7B5%7D%24%24%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20The%20Cross-Correlation%20function%0A%0A%20%20%20%20TTo%20compute%20the%20cross-covariance%20between%20two%20vectors%20%24x_t%24%20and%20%24y_t%24%2C%20at%20the%20%24k%24%20lag%2C%20we%20do%3A%0A%0A%20%20%20%20%24%24cv_%7Bx%20y%7D(k)%3D%5Cfrac%7B1%7D%7Bn-1%7D%20%5Csum_%7Bt%3D1%7D%5E%7Bn-k%7D%5Cleft(x_%7Bt%2Bk%7D-%5Cbar%7Bx%7D%5Cright)%5Cleft(y_%7Bt%7D-%5Cbar%7By%7D%5Cright)%20%5Ctag%7B6%7D%24%24%0A%0A%20%20%20%20where%2C%20according%20to%20our%20notation%20above%2C%20%24%5C%7B%5Coverline%7Bx%7D%2C%5Coverline%7By%7D%5C%7D%24%20are%20the%20sample%20means%20of%20%24x_t%24%20and%20%24y_t%24%2C%20respectively%2C%20while%20%24k%24%20is%20a%20positive%20lag.%0A%0A%20%20%20%20The%20cross-correlation%20function%20is%20a%20normalized%20version%20of%20the%20cross-variance%20by%20dividing%20it%20by%20the%20sample%20variances%20of%20%24x_%7Bt%7D%24%20and%20%24y_%7Bt%7D%24.%20As%20the%20sample%20variances%20also%20have%20%24n-1%24%20in%20their%20denominators%2C%20the%20terms%20%24n-1%24%20cancel%20out%20and%20the%20cross-correlation%20function%20can%20be%20computed%20using%20the%20formula%3A%0A%0A%20%20%20%20%24%24cr_%7Bx%20y%7D(k)%3D%20%5Cfrac%7B%5Csum_%7Bt%3D1%7D%5E%7Bn-k%7D%5Cleft(x_%7Bt%2Bk%7D-%5Cbar%7Bx%7D%5Cright)%5Cleft(y_t-%5Cbar%7By%7D%5Cright)%7D%20%7B%5Csqrt%7B%5Csum_%7Bt%3D1%7D%5En%5Cleft(x_t-%5Cbar%7Bx%7D%5Cright)%5E2%7D%20%5Ctimes%20%5Csqrt%7B%5Csum_%7Bt%3D1%7D%5En%5Cleft(y_t-%5Cbar%7By%7D%5Cright)%5E2%7D%7D%20%5Ctag%7B7%7D%24%24%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%23%20Python%20functions%20for%20basic%20statistical%20measures%0A%0A%20%20%20%20%7C%20Measure%20%7C%20Function%20%7C%20Package%20%7C%0A%20%20%20%20%7C%3A---%7C%3A---%7C%3A---%7C%0A%20%20%20%20%7C%20Mean%20%7C%20%60np.mean(x)%60%20%7C%20NumPy%20%7C%0A%20%20%20%20%7C%20Standard%20deviation%20%7C%20%60np.std(x%2C%20ddof%3D1)%60%20%7C%20NumPy%20%7C%0A%20%20%20%20%7C%20Auto-correlation%20%7C%20%60acf(x%2C%20nlags%3D...)%60%20%7C%20statsmodels%20%7C%0A%20%20%20%20%7C%20Cross-correlation%20%7C%20%60crosscor(x%2C%20y%2C%20lags)%60%20*(custom)*%20%7C%20%E2%80%94%20%7C%0A%20%20%20%20%7C%20Correlation%20coefficient%20%7C%20%60np.corrcoef(x%2C%20y)%5B0%2C1%5D%60%20%7C%20NumPy%20%7C%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Using%20random%20observations%0A%0A%20%20%20%20%22*In%20discrete%20time%2C%20white%20noise%20is%20a%20discrete%20signal%20whose%20samples%20are%20regarded%20as%20a%20sequence%20of%20serially%0A%20%20%20%20uncorrelated%20random%20observations%20with%20zero%20mean%20and%20finite%20variance%3B%20a%20single%20realization%20of%20white%20noise%0A%20%20%20%20is%20a%20random%20shock*%22.%20%E2%80%94%20Wikipedia%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np)%3A%0A%20%20%20%20np.random.seed(100)%0A%20%20%20%20noise%20%3D%20np.random.randn(500)%0A%20%20%20%20shrek%20%3D%20np.random.randn(500)%0A%20%20%20%20return%20noise%2C%20shrek%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(go%2C%20noise)%3A%0A%20%20%20%20fig_wn%20%3D%20go.Figure(%0A%20%20%20%20%20%20%20%20data%3D%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20y%3Dnoise%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22markers%2Blines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(size%3D6%2C%20color%3D%22blue%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22blueviolet%22%2C%20width%3D0.5)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20)%0A%20%20%20%20fig_wn.update_layout(%0A%20%20%20%20%20%20%20%20title_text%3D%22White%20noise%22%2C%0A%20%20%20%20%20%20%20%20title_x%3D0.5%2C%0A%20%20%20%20%20%20%20%20hovermode%3D%22x%22%2C%0A%20%20%20%20%20%20%20%20height%3D450%2C%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D70%2C%20r%3D60%2C%20t%3D70%2C%20b%3D60)%20%20%0A%20%20%20%20)%0A%20%20%20%20fig_wn%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(noise%2C%20np)%3A%0A%20%20%20%20mean_noise%20%3D%20np.mean(noise)%0A%20%20%20%20mean_noise%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(noise%2C%20np)%3A%0A%20%20%20%20std_noise%20%3D%20np.std(noise%2C%20ddof%3D1)%0A%20%20%20%20std_noise%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(acf%2C%20noise)%3A%0A%20%20%20%20acf_noise%20%3D%20acf(noise%2C%20nlags%3D25%2C%20fft%3DTrue)%0A%20%20%20%20acf_noise%0A%20%20%20%20return%20(acf_noise%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(acf_noise%2C%20go)%3A%0A%20%20%20%20fig_acf_noise%20%3D%20go.Figure(%0A%20%20%20%20%20%20%20%20data%3D%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20y%3Dacf_noise%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22markers%2Blines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(symbol%3D%22circle%22%2C%20size%3D7%2C%20color%3D%22blue%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22blueviolet%22%2C%20width%3D0.6)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20)%0A%20%20%20%20fig_acf_noise.update_layout(%0A%20%20%20%20%20%20%20%20title_text%3D%22Autocorrelation%20function%3A%20white%20noise%22%2C%0A%20%20%20%20%20%20%20%20title_x%3D0.5%2C%0A%20%20%20%20%20%20%20%20height%3D450%2C%0A%20%20%20%20%20%20%20%20xaxis_title%3D%22lag%22%2C%0A%20%20%20%20%20%20%20%20yaxis_title%3D%22Autocorrelation%22%2C%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D70%2C%20r%3D60%2C%20t%3D70%2C%20b%3D60)%20%20%0A%20%20%20%20)%0A%20%20%20%20fig_acf_noise%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(go%2C%20shrek)%3A%0A%20%20%20%20lag_amount%20%3D%201%20%20%23%20try%201%2C%2010%2C%2020%2C%20100%0A%20%20%20%20shrek_lagged%20%3D%20shrek%5B%3A-lag_amount%5D%20if%20lag_amount%20%3E%200%20else%20shrek%0A%20%20%20%20shrek_orig%20%20%20%3D%20shrek%5Blag_amount%3A%5D%20%20if%20lag_amount%20%3E%200%20else%20shrek%0A%0A%20%20%20%20fig_shrek%20%3D%20go.Figure(%0A%20%20%20%20%20%20%20%20data%3D%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20x%3Dshrek_orig%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20y%3Dshrek_lagged%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22markers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(symbol%3D%22circle%22%2C%20size%3D6%2C%20color%3D%22blue%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20)%0A%20%20%20%20fig_shrek.update_layout(%0A%20%20%20%20%20%20%20%20width%3D600%2C%20height%3D600%2C%0A%20%20%20%20%20%20%20%20title_text%3D%22shrek%20vs%20shrek_lagged%22%2C%0A%20%20%20%20%20%20%20%20title_x%3D0.5%2C%0A%20%20%20%20%20%20%20%20xaxis_title%3D%22shrek%22%2C%0A%20%20%20%20%20%20%20%20yaxis_title%3D%22shrek_lagged%22%2C%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D70%2C%20r%3D60%2C%20t%3D70%2C%20b%3D60)%20%20%0A%20%20%20%20)%0A%20%20%20%20fig_shrek%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(go%2C%20noise%2C%20shrek)%3A%0A%20%20%20%20fig_ns%20%3D%20go.Figure(%0A%20%20%20%20%20%20%20%20data%3D%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20x%3Dnoise%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20y%3Dshrek%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22markers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(symbol%3D%22circle%22%2C%20size%3D6%2C%20color%3D%22maroon%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20)%0A%20%20%20%20fig_ns.update_layout(%0A%20%20%20%20%20%20%20%20width%3D600%2C%20height%3D600%2C%0A%20%20%20%20%20%20%20%20title_text%3D%22noise%20vs%20shrek%22%2C%0A%20%20%20%20%20%20%20%20title_x%3D0.5%2C%0A%20%20%20%20%20%20%20%20xaxis_title%3D%22noise%22%2C%0A%20%20%20%20%20%20%20%20yaxis_title%3D%22shrek%22%2C%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D70%2C%20r%3D60%2C%20t%3D70%2C%20b%3D60)%20%20%0A%20%20%20%20)%0A%20%20%20%20fig_ns%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np)%3A%0A%20%20%20%20clags%20%3D%20np.arange(-100%2C%20101)%20%20%23%20-100%3A100%20inclusive%0A%20%20%20%20return%20(clags%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(clags%2C%20crosscor%2C%20go%2C%20noise%2C%20shrek)%3A%0A%20%20%20%20ccf_ns%20%3D%20crosscor(noise%2C%20shrek%2C%20clags)%20%23uses%20the%20crosscor%20function%20that%20can%20be%20found%20at%20the%20end%20of%20this%20notebook%0A%0A%20%20%20%20fig_cc_ns%20%3D%20go.Figure(%0A%20%20%20%20%20%20%20%20data%3D%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20x%3Dclags%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20y%3Dccf_ns%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22markers%2Blines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(symbol%3D%22circle%22%2C%20size%3D6%2C%20color%3D%22blue%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(width%3D0.3)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20)%0A%20%20%20%20fig_cc_ns.update_layout(%0A%20%20%20%20%20%20%20%20title_text%3D%22Cross-correlation%20between%20noise%20and%20shrek%22%2C%0A%20%20%20%20%20%20%20%20title_x%3D0.5%2C%0A%20%20%20%20%20%20%20%20height%3D450%2C%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D70%2C%20r%3D60%2C%20t%3D70%2C%20b%3D60)%20%20%0A%20%20%20%20)%0A%20%20%20%20fig_cc_ns%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Using%20data%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(pd%2C%20preview)%3A%0A%20%20%20%20inflation%20%3D%20pd.read_csv(%22ECB%20Data%20Inflation.csv%22%2C%20parse_dates%3D%5B%22Months%22%5D)%0A%20%20%20%20preview(inflation)%0A%20%20%20%20return%20(inflation%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%23%20Select%20a%20subset%20of%20a%20data%20frame%20based%20on%20years%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(inflation%2C%20pd)%3A%0A%20%20%20%20first_date%20%3D%20pd.Timestamp(%221997-02-28%22)%0A%20%20%20%20last_date%20%20%3D%20pd.Timestamp(%221997-09-30%22)%0A%20%20%20%20subset_inflation%20%3D%20inflation%5B%0A%20%20%20%20%20%20%20%20(inflation%5B%22Months%22%5D%20%3E%3D%20first_date)%20%26%20(inflation%5B%22Months%22%5D%20%3C%3D%20last_date)%0A%20%20%20%20%5D%0A%20%20%20%20subset_inflation%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%23%20Another%20way%20of%20doing%20it%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(inflation)%3A%0A%20%20%20%20subset_by_year%20%3D%20inflation%5B%0A%20%20%20%20%20%20%20%20(inflation%5B%22Months%22%5D.dt.year%20%3E%3D%201997)%20%26%20(inflation%5B%22Months%22%5D.dt.year%20%3C%3D%201999)%0A%20%20%20%20%5D%0A%20%20%20%20subset_by_year%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%23%20%E2%80%A6%20still%20another%20way%20of%20doing%20it%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(inflation%2C%20pd)%3A%0A%20%20%20%20subset_range%20%3D%20inflation%5B%0A%20%20%20%20%20%20%20%20(inflation%5B%22Months%22%5D%20%3E%3D%20pd.Timestamp(%221997-06-30%22))%0A%20%20%20%20%20%20%20%20%26%20(inflation%5B%22Months%22%5D%20%3C%3D%20pd.Timestamp(%221998-06-30%22))%0A%20%20%20%20%5D%0A%20%20%20%20subset_range%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20How%20to%20create%20an%20artificial%20data%20set%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20pd)%3A%0A%20%20%20%20dates%20%3D%20pd.date_range(start%3D%221997-01-31%22%2C%20end%3D%222023-12-31%22%2C%20freq%3D%22ME%22)%0A%20%20%20%20df%20%3D%20pd.DataFrame(%7B%22Date%22%3A%20dates%2C%20%22Value%22%3A%20np.random.rand(len(dates))%7D)%0A%20%20%20%20df%0A%20%20%20%20return%20(df%2C)%0A%0A%0A%40app.cell%0Adef%20_(df%2C%20pd)%3A%0A%20%20%20%20start_date%20%3D%20pd.Timestamp(%222002-07-31%22)%0A%20%20%20%20end_date%20%20%20%3D%20pd.Timestamp(%222005-06-30%22)%0A%20%20%20%20subset_df%20%20%3D%20df%5B(df%5B%22Date%22%5D%20%3E%3D%20start_date)%20%26%20(df%5B%22Date%22%5D%20%3C%3D%20end_date)%5D%0A%20%20%20%20subset_df%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(inflation)%3A%0A%20%20%20%20inflation.describe()%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(inflation)%3A%0A%20%20%20%20inflation.describe(include%3D%22all%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(go%2C%20inflation)%3A%0A%20%20%20%20fig_ez_pt%20%3D%20go.Figure()%0A%0A%20%20%20%20fig_ez_pt.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3Dinflation%5B%22Months%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dinflation%5B%22EuroArea%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22EuroZone%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22markers%2Blines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(symbol%3D%22circle%22%2C%20size%3D5%2C%20color%3D%22darkblue%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(width%3D0.5)%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%20%20%20%20fig_ez_pt.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3Dinflation%5B%22Months%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dinflation%5B%22Portugal%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Portugal%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22markers%2Blines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(symbol%3D%22circle%22%2C%20size%3D5%2C%20color%3D%22maroon%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(width%3D0.5)%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%0A%20%20%20%20fig_ez_pt.update_layout(%0A%20%20%20%20%20%20%20%20height%3D450%2C%0A%20%20%20%20%20%20%20%20title%20%3D%22Inflation%20in%20the%20EuroZone%20and%20Portugal%22%2C%0A%20%20%20%20%20%20%20%20title_x%3D0.5%2C%0A%20%20%20%20%20%20%20%20hovermode%3D%22x%22%2C%0A%20%20%20%20%20%20%20%20xaxis%3Ddict(%0A%20%20%20%20%20%20%20%20%20%20%20%20title%3D%22Monthly%20observations%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20tickformat%3D%22%25Y%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20hoverformat%3D%22%25Y-M%25m%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20yaxis_title%3D%22Percentage%20points%22%2C%0A%20%20%20%20%20%20%20%20title_font_size%3D16%2C%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D70%2C%20r%3D60%2C%20t%3D70%2C%20b%3D60)%20%20%0A%20%20%20%20)%0A%20%20%20%20fig_ez_pt%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20To%20obtain%20the%20autocorrelation%20function%2C%20we%20can%20use%20the%20%60acf%60%20function%20from%20the%20%60statsmodels%60%20package%2C%20as%20follows%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(acf%2C%20go%2C%20inflation)%3A%0A%20%20%20%20acf_ez%20%3D%20acf(inflation%5B%22EuroArea%22%5D.values%2C%20nlags%3D25%20-%201%2C%20fft%3DTrue)%0A%0A%20%20%20%20fig_acf_ez%20%3D%20go.Figure(%0A%20%20%20%20%20%20%20%20data%3D%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20y%3Dacf_ez%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22markers%2Blines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(symbol%3D%22circle%22%2C%20size%3D7%2C%20color%3D%22blue%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(width%3D0.3)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20)%0A%20%20%20%20fig_acf_ez.update_layout(%0A%20%20%20%20%20%20%20%20title_text%3D%22Auto-correlation%20of%20inflation%20in%20the%20EZ%22%2C%0A%20%20%20%20%20%20%20%20title_x%3D0.5%2C%0A%20%20%20%20%20%20%20%20hovermode%3D%22x%22%2C%0A%20%20%20%20%20%20%20%20height%3D450%2C%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D70%2C%20r%3D60%2C%20t%3D70%2C%20b%3D60)%0A%20%20%20%20)%0A%20%20%20%20fig_acf_ez%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20To%20plot%20Inflation%20against%20lagged-Inflation%2C%20we%20can%20use%20a%20slider%20to%20see%20what%20happens%20with%20different%20lags.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20lags_slider%20%3D%20mo.ui.slider(-36%2C%2036%2C%20value%3D0%2C%20step%3D1%2C%20label%3D%22lags%22%2C%20show_value%3DTrue)%0A%20%20%20%20lags_slider%0A%20%20%20%20return%20(lags_slider%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(go%2C%20inflation%2C%20lags_slider)%3A%0A%20%20%20%20EZ%20%3D%20inflation%5B%22EuroArea%22%5D.values%0A%20%20%20%20lag_val%20%3D%20lags_slider.value%0A%0A%20%20%20%20if%20lag_val%20%3E%200%3A%0A%20%20%20%20%20%20%20%20ez_orig%20%20%20%3D%20EZ%5Blag_val%3A%5D%0A%20%20%20%20%20%20%20%20ez_lagged%20%3D%20EZ%5B%3A-lag_val%5D%0A%20%20%20%20elif%20lag_val%20%3C%200%3A%0A%20%20%20%20%20%20%20%20ez_orig%20%20%20%3D%20EZ%5B%3Alag_val%5D%0A%20%20%20%20%20%20%20%20ez_lagged%20%3D%20EZ%5B-lag_val%3A%5D%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20ez_orig%20%20%20%3D%20EZ%0A%20%20%20%20%20%20%20%20ez_lagged%20%3D%20EZ%0A%0A%20%20%20%20fig_ez_lag%20%3D%20go.Figure(%0A%20%20%20%20%20%20%20%20data%3D%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20x%3Dez_orig%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20y%3Dez_lagged%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22markers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(symbol%3D%22circle%22%2C%20size%3D6%2C%20color%3D%22%237a5135%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20)%0A%20%20%20%20fig_ez_lag.update_layout(%0A%20%20%20%20%20%20%20%20width%3D600%2C%20height%3D600%2C%0A%20%20%20%20%20%20%20%20title_text%3D%22Inflation%20vs%20Lagged-Inflation%3A%20EZ%22%2C%0A%20%20%20%20%20%20%20%20title_x%3D0.5%2C%0A%20%20%20%20%20%20%20%20xaxis_title%3D%22EZ%20inflation%22%2C%0A%20%20%20%20%20%20%20%20yaxis_title%3D%22EZ%20inflation%20(lagged)%22%2C%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D70%2C%20r%3D60%2C%20t%3D70%2C%20b%3D60)%0A%20%20%20%20)%0A%20%20%20%20fig_ez_lag%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(go%2C%20inflation)%3A%0A%20%20%20%20fig_ez_pt_scatter%20%3D%20go.Figure(%0A%20%20%20%20%20%20%20%20data%3D%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20x%3Dinflation%5B%22EuroArea%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20y%3Dinflation%5B%22Portugal%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22markers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(symbol%3D%22circle%22%2C%20size%3D6%2C%20color%3D%22blue%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20)%0A%20%20%20%20fig_ez_pt_scatter.update_layout(%0A%20%20%20%20%20%20%20%20width%3D600%2C%20height%3D600%2C%0A%20%20%20%20%20%20%20%20title_text%3D%22Inflation%20in%20the%20EuroZone%20and%20Portugal%22%2C%0A%20%20%20%20%20%20%20%20title_x%3D0.5%2C%0A%20%20%20%20%20%20%20%20xaxis_title%3D%22EuroZone%22%2C%0A%20%20%20%20%20%20%20%20yaxis_title%3D%22Portugal%22%2C%0A%20%20%20%20%20%20%20%20yaxis_range%3D%5B-1.0%2C%205.5%5D%2C%0A%20%20%20%20%20%20%20%20xaxis_range%3D%5B-1.0%2C%206.5%5D%2C%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D70%2C%20r%3D60%2C%20t%3D70%2C%20b%3D60)%0A%20%20%20%20)%0A%20%20%20%20fig_ez_pt_scatter%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(inflation%2C%20np)%3A%0A%20%20%20%20cor_ez_pt%20%3D%20np.corrcoef(inflation%5B%22EuroArea%22%5D%2C%20inflation%5B%22Portugal%22%5D)%5B0%2C%201%5D%0A%20%20%20%20cor_ez_pt%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(clags%2C%20crosscor%2C%20go%2C%20inflation)%3A%0A%20%20%20%20ccf_ez_pt%20%3D%20crosscor(%0A%20%20%20%20%20%20%20%20inflation%5B%22EuroArea%22%5D.values%2C%0A%20%20%20%20%20%20%20%20inflation%5B%22Portugal%22%5D.values%2C%0A%20%20%20%20%20%20%20%20clags%2C%0A%20%20%20%20)%0A%0A%20%20%20%20fig_cc_ez_pt%20%3D%20go.Figure(%0A%20%20%20%20%20%20%20%20data%3D%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20x%3Dclags%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20y%3Dccf_ez_pt%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22markers%2Blines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(symbol%3D%22circle%22%2C%20size%3D6%2C%20color%3D%22blue%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(width%3D0.3)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20)%0A%20%20%20%20fig_cc_ez_pt.update_layout(%0A%20%20%20%20%20%20%20%20title_text%3D%22Cross-correlation%20between%20inflation%20in%20the%20EuroZone%20and%20Portugal%22%2C%0A%20%20%20%20%20%20%20%20title_x%3D0.5%2C%0A%20%20%20%20%20%20%20%20height%3D450%2C%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D70%2C%20r%3D60%2C%20t%3D70%2C%20b%3D60)%0A%20%20%20%20)%0A%20%20%20%20fig_cc_ez_pt%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(clags%2C%20crosscor%2C%20inflation%2C%20pd)%3A%0A%20%20%20%20ccf_python%20%3D%20crosscor(%0A%20%20%20%20%20%20%20%20inflation%5B%22EuroArea%22%5D.values%2C%0A%20%20%20%20%20%20%20%20inflation%5B%22Portugal%22%5D.values%2C%0A%20%20%20%20%20%20%20%20clags%2C%0A%20%20%20%20)%0A%20%20%20%20pd.DataFrame(%7B%22ccf_python%22%3A%20ccf_python%7D).to_csv(%22ccf_python.csv%22%2C%20index%3DFalse)%0A%20%20%20%20type(ccf_python)%0A%20%20%20%20ccf_python%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20---%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%202.%20Stability%20of%20dynamic%20systems%0A%20%20%20%20%3Cbr%3E%0A%20%20%20%20A%20dynamical%20process%20displays%20the%20evolution%20of%20one%20variable%20(or%20a%20set%20of%20variables)%20across%20time%2C%20starting%20from%0A%20%20%20%20a%20particular%20initial%20value%20(or%20initial%20values)%2C%20resulting%20from%20the%20impact%20of%20a%20set%20of%20given%20forces.%0A%0A%20%20%20%20It%20has%20three%20fundamental%20ingredients%3A%0A%0A%20%20%20%20-%20An%20initial%20condition%0A%20%20%20%20-%20Transitional%20dynamics%0A%20%20%20%20-%20A%20steady%20state.%0A%0A%20%20%20%20See%20the%20following%20figure%20for%20a%20graphical%20example.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.Html(%0A%20%20%20%20%20%20%20%20'%3Cimg%20src%3D%22https%3A%2F%2Fvivaldomendes.org%2Fimages%2Fdepot%2Ftransitional_dynamics.png%22%20width%3D%22650%22%20style%3D%22max-width%3A100%25%3B%22%3E'%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Consider%20the%20simplest%20possible%20dynamic%20process%3A%20an%20Auto-Regressive%20process%20of%20first%20order%2C%20usually%20called%0A%20%20%20%20simply%20by%20an%20AR(1)%3A%0A%0A%20%20%20%20%24%24y_%7Bt%2B1%7D%3D%20a%20%2B%20%5Crho%20%5Ccdot%20y_t%20%5Ctag%7B8%7D%24%24%0A%0A%20%20%20%20where%20%24a%24%20is%20a%20constant%20(or%20an%20exogenous%20force)%20and%20%24%5Crho%24%20is%20a%20parameter.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%F0%9F%91%89%20a)%20Pass%20the%20model's%20data%20%24(a%2C%5Crho)%24%20into%20the%20notebook.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20a%20%3D%2010%0A%20%20%20%20%CF%81%20%3D%200.5%0A%20%20%20%20return%20a%2C%20%CF%81%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%F0%9F%91%89%20**b)**%20Calculate%20the%20steady%20state%20of%20this%20model.%20%20Remember%20that%20the%20steady%20state%20is%20obtained%20by%20imposing%0A%20%20%20%20the%20condition%20%24y_%7Bt%2B1%7D%20%3D%20y_t%20%3D%20%5Cbar%7By%7D%24%20upon%20our%20AR(1).%20%20Therefore%2C%0A%0A%20%20%20%20%24%24%5Cbar%7By%7D%3Da%2B%5Crho%20%5Cbar%7By%7D%20%5CRightarrow%20%5Cbar%7By%7D%3D%5Cfrac%7B1%7D%7B1-%5Crho%7Da%3D(1-%5Crho)%5E%7B-1%7D%20%5Ccdot%20a%20%5Ctag%7B9%7D%24%24%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(a%2C%20%CF%81)%3A%0A%20%20%20%20%C8%B3%20%3D%20((1%20-%20%CF%81)**(-1))%20*%20a%0A%20%20%20%20%C8%B3%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%3E%20**Answer%20(b)**%0A%20%20%20%20%3E%0A%20%20%20%20%3E%20Here%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%F0%9F%91%89%20**c)**%20Check%20the%20stability%20of%20this%20model.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%CF%81)%3A%0A%20%20%20%20%23%20For%20a%20scalar%20AR(1)%20the%20single%20%22eigenvalue%22%20is%20rho%20itself%0A%20%20%20%20eigenvalue_ar1%20%3D%20%CF%81%0A%20%20%20%20eigenvalue_ar1%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%3E%20**Answer%20(c)**%0A%20%20%20%20%3E%0A%20%20%20%20%3E%20Here%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%F0%9F%91%89%20**d)**%20Simulate%20the%20model%20for%20a%20period%20of%2030%20iterations%2C%20with%20an%20initial%20condition%20of%20%24y_1%20%3D%204%24.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(a%2C%20np%2C%20%CF%81)%3A%0A%20%20%20%20n%20%3D%2030%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20number%20of%20iterations%20we%20want%20to%20simulate%0A%20%20%20%20y1%20%3D%204%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20initial%20condition%0A%20%20%20%20y%20%3D%20np.zeros(n)%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20Allocating%20space%20for%20y%0A%20%20%20%20y%5B0%5D%20%3D%20y1%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20initial%20condition%20goes%20in%20position%200%0A%0A%20%20%20%20for%20t%20in%20range(n%20-%201)%3A%20%20%20%20%20%20%20%20%20%20%23%20begins%20the%20%22for%22%20loop%0A%20%20%20%20%20%20%20%20y%5Bt%2B1%5D%20%3D%20a%20%2B%20%CF%81%20*%20y%5Bt%5D%20%20%20%20%20%20%20%23%20the%20loop%20works%20through%20eq.%20(8)%3A%20y(t%2B1)%20%3D%20a%20%2B%20%CF%81y(t)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20ends%20the%20for%20loop%0A%20%20%20%20return%20(y%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(go%2C%20y)%3A%0A%20%20%20%20fig_ar1%20%3D%20go.Figure(%0A%20%20%20%20%20%20%20%20data%3D%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20y%3Dy%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%2Bmarkers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(symbol%3D%22circle%22%2C%20size%3D7%2C%20color%3D%22blue%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(width%3D0.5)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20)%0A%20%20%20%20fig_ar1.update_layout(%0A%20%20%20%20%20%20%20%20title_text%3D%22The%20evolution%20of%20an%20AR(1)%20process%22%2C%0A%20%20%20%20%20%20%20%20title_x%3D0.5%2C%0A%20%20%20%20%20%20%20%20xaxis_title%3D%22time%22%2C%0A%20%20%20%20%20%20%20%20yaxis_title%3D%22y(t)%22%2C%0A%20%20%20%20%20%20%20%20height%3D450%2C%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D70%2C%20r%3D60%2C%20t%3D70%2C%20b%3D60)%0A%20%20%20%20)%0A%20%20%20%20fig_ar1%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%201%3A%20add%20noise%0A%0A%20%20%20%20Introduce%20a%20series%20of%20stochastic%20(or%20random)%20shocks%20to%20the%20AR(1)%20process%20above.%0A%20%20%20%20This%20stochastic%20variable%20is%20defined%20below%20as%20%24%5Cvarepsilon%24%20and%20the%20process%20is%20written%20as%3A%0A%0A%20%20%20%20%24%24y_%7Bt%2B1%7D%3D%20a%20%2B%20%5Crho%20%5Ccdot%20y_t%20%2B%20%5Cvarepsilon_%7Bt%2B1%7D%20%5Ctag%7B10%7D%24%24%0A%0A%20%20%20%20The%20impact%20of%20this%20random%20element%20can%20be%20obtained%20by%20adding%20the%20term%20%60%CE%B5%5Bt%2B1%5D%60%20to%20the%20loop%20above.%0A%20%20%20%20Now%2C%20the%20loop%20should%20be%20written%20as%3A%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20%20%20%20%20y%5Bt%20%2B%201%5D%20%3D%20a%20%2B%20rho%20*%20y%5Bt%5D%20%2B%20epsilon%5Bt%20%2B%201%5D%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20To%20avoid%20multiple%20definitions%20in%20the%20notebook%2C%20let%20us%20name%20our%20variable%20%60y_noise%60%20instead%20of%20%60y%60%2C%0A%20%20%20%20where%20%60y_noise%60%20represents%20%22y%20with%20noise%22.%20%20Everything%20else%20will%20remain%20the%20same%2C%20apart%20from%20telling%20the%0A%20%20%20%20notebook%20the%20kind%20of%20noise%20we%20want%20in%20the%20model.%20%20In%20this%20case%2C%20we%20will%20use%20%60n%60%20random%20normally%20distributed%0A%20%20%20%20observations%20(%60np.random.randn(m)%60).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(a%2C%20np%2C%20%CF%81)%3A%0A%20%20%20%20m%20%20%3D%2030%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20number%20of%20iterations%20we%20want%20to%20simulate%0A%20%20%20%20y_noise1%20%3D%204%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20initial%20condition%0A%20%20%20%20%F0%9D%9C%BA%20%3D%20np.random.randn(m)%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20Random%20shocks%0A%20%20%20%20y_noise%20%3D%20np.zeros(m)%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20Allocating%20space%20for%20y%0A%20%20%20%20y_noise%5B0%5D%20%3D%20y_noise1%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20initial%20condition%20goes%20in%20position%200%0A%0A%20%20%20%20for%20_t%20in%20range(m%20%20-%201)%3A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20begins%20the%20%22for%22%20loop%0A%20%20%20%20%20%20%20%20y_noise%5B_t%2B1%5D%20%3D%20a%20%2B%20%CF%81%20*%20y_noise%5B_t%5D%20%2B%20%F0%9D%9C%BA%5B_t%2B1%5D%20%20%23%20the%20loop%20works%20through%20eq.%20(10)%3A%20y(t%2B1)%20%3D%20a%20%2B%20%CF%81y(t)%20%2B%20%CE%B5(t%2B1)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20ends%20the%20for%20loop%0A%20%20%20%20return%20(y_noise%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Now%2C%20we%20can%20plot%20our%20variable%20%24y%24%20with%20noise%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(go%2C%20y_noise)%3A%0A%20%20%20%20fig_noise%20%3D%20go.Figure(%0A%20%20%20%20%20%20%20%20data%3D%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20y%3Dy_noise%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%2Bmarkers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(symbol%3D%22circle%22%2C%20size%3D7%2C%20color%3D%22blue%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(width%3D0.5)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20)%0A%20%20%20%20fig_noise.update_layout(%0A%20%20%20%20%20%20%20%20height%3D450%2C%0A%20%20%20%20%20%20%20%20title_text%3D%22The%20evolution%20of%20an%20AR(1)%20process%20with%20noise%22%2C%0A%20%20%20%20%20%20%20%20title_x%3D0.5%2C%0A%20%20%20%20%20%20%20%20xaxis_title%3D%22time%22%2C%0A%20%20%20%20%20%20%20%20yaxis_title%3D%22y(t)%22%2C%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D70%2C%20r%3D60%2C%20t%3D70%2C%20b%3D60)%0A%20%20%20%20)%0A%20%20%20%20fig_noise%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%202%3A%20public%20debt%0A%0A%20%20%20%20The%20sustainability%20of%20public%20debt%20is%20given%20by%20this%20simple%20linear%20difference%20equation%3A%0A%0A%20%20%20%20%24%24d_%7Bt%2B1%7D%3Dp%2B%5Cleft(%5Cfrac%7B1%2Br%7D%7B1%2Bg%7D%5Cright)%20d_%7Bt%7D%20%5Ctag%7B11%7D%24%24%0A%0A%20%20%20%20where%0A%0A%20%20%20%20-%20%24d_t%24%20is%20the%20public%20debt%20as%20a%20percentage%20of%20GDP%0A%20%20%20%20-%20%24p%24%20is%20the%20primary%20deficit%20as%20a%20percentage%20of%20GDP%0A%20%20%20%20-%20%24r%24%20is%20the%20real%20interest%20rate%20paid%20on%20public%20debt%0A%20%20%20%20-%20%24g%24%20is%20the%20growth%20rate%20of%20real%20GDP%0A%0A%20%20%20%20Let%20us%20simulate%20the%20dynamics%20associated%20with%20this%20equation%20using%20a%20%3Ckbd%3Efor%3C%2Fkbd%3E%20loop.%20%20By%20changing%20the%20sliders%20below%2C%0A%20%20%20%20present%20a%20scenario%20that%20is%20capable%20of%20reducing%20the%20level%20of%20%24d_t%24%20to%2080%20%25%20of%20GDP%20in%20no%20more%20than%20fifty%20years.%20Notice%20that%20the%20sliders%20present%20values%20in%20terms%20of%20percentage%20points.%20Foe%20example%2C%20%24r%3D1.5%24%20means%20that%20%24r%24%20is%20equal%20to%201.5%20percentage%20points.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20p_slider%20%20%3D%20mo.ui.slider(-5.0%2C%205.0%2C%20%20value%3D0.5%2C%20%20step%3D0.1%2C%20%20label%3D%22p%20%20%E2%80%A6%20Primary%20deficit%22%2C%20%20%20%20%20%20show_value%3DTrue)%0A%20%20%20%20r_slider%20%20%3D%20mo.ui.slider(%200.0%2C%204.0%2C%20%20value%3D1.5%2C%20%20step%3D0.05%2C%20label%3D%22r%20%20%E2%80%A6%20Real%20interest%20rate%22%2C%20%20%20%20show_value%3DTrue)%0A%20%20%20%20g_slider%20%20%3D%20mo.ui.slider(%200.0%2C%205.0%2C%20%20value%3D2.0%2C%20%20step%3D0.05%2C%20label%3D%22g%20%20%E2%80%A6%20GDP%20growth%20rate%22%2C%20%20%20%20%20%20%20show_value%3DTrue)%0A%20%20%20%20d1_slider%20%3D%20mo.ui.slider(%200.0%2C%202.0%2C%20%20value%3D1.2%2C%20%20step%3D0.1%2C%20%20label%3D%22d1%20%E2%80%A6%20Initial%20value%22%2C%20%20%20%20%20%20%20%20%20show_value%3DTrue)%0A%0A%20%20%20%20mo.hstack(%5B%0A%20%20%20%20%20%20%20%20mo.vstack(%5Bp_slider%2C%20%20r_slider%5D)%2C%0A%20%20%20%20%20%20%20%20mo.vstack(%5Bg_slider%2C%20d1_slider%5D)%2C%0A%20%20%20%20%5D%2C%20justify%3D%22center%22%2C%20gap%3D0.1)%0A%20%20%20%20return%20d1_slider%2C%20g_slider%2C%20p_slider%2C%20r_slider%0A%0A%0A%40app.cell%0Adef%20_(d1_slider%2C%20g_slider%2C%20np%2C%20p_slider%2C%20r_slider)%3A%0A%20%20%20%20N_debt%20%3D%20500%0A%20%20%20%20p_val%20%20%3D%20p_slider.value%0A%20%20%20%20r_val%20%20%3D%20r_slider.value%0A%20%20%20%20g_val%20%20%3D%20g_slider.value%0A%20%20%20%20d1_val%20%3D%20d1_slider.value%0A%0A%20%20%20%20d_debt%20%3D%20np.zeros(N_debt)%0A%20%20%20%20d_debt%5B0%5D%20%3D%20d1_val%0A%0A%20%20%20%20for%20_t%20in%20range(N_debt%20-%201)%3A%0A%20%20%20%20%20%20%20%20d_debt%5B_t%20%2B%201%5D%20%3D%20p_val%20%2F%20100%20%2B%20((1%20%2B%20r_val%20%2F%20100)%20%2F%20(1%20%2B%20g_val%20%2F%20100))%20*%20d_debt%5B_t%5D%0A%20%20%20%20return%20(d_debt%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(d_debt%2C%20go)%3A%0A%20%20%20%20fig_debt%20%3D%20go.Figure(%0A%20%20%20%20%20%20%20%20data%3D%5Bgo.Scatter(y%3Dd_debt%2C%20mode%3D%22lines%22%2C%20line%3Ddict(width%3D2.5))%5D%0A%20%20%20%20)%0A%20%20%20%20fig_debt.update_layout(%0A%20%20%20%20%20%20%20%20height%3D450%2C%0A%20%20%20%20%20%20%20%20title%3D%22The%20evolution%20of%20Public%20Debt%20as%20%25%20of%20GDP%22%2C%0A%20%20%20%20%20%20%20%20title_x%3D0.5%2C%0A%20%20%20%20%20%20%20%20xaxis_title%3D%22Years%22%2C%0A%20%20%20%20%20%20%20%20yaxis_title%3D%22Debt%20%2F%20GDP%22%2C%0A%20%20%20%20%20%20%20%20title_font_size%3D16%2C%0A%20%20%20%20%20%20%20%20hovermode%3D%22x%22%2C%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D70%2C%20r%3D60%2C%20t%3D70%2C%20b%3D60)%0A%20%20%20%20)%0A%20%20%20%20fig_debt%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%203%3A%20add%20noise%20to%20the%20primary%20deficit%0A%0A%20%20%20%20To%20add%20noise%20to%20the%20dynamic%20process%20that%20describes%20the%20sustainability%20of%20public%20debt%2C%20add%20the%20term%0A%20%20%20%20%24%5Cepsilon_%7Bt%2B1%7D%24%20to%20eq.%20(11)%2C%20where%20%24%5Cepsilon%24%20is%20meant%20to%20represent%20a%20sequence%20of%20random%20factors%20that%0A%20%20%20%20affect%20the%20level%20of%20public%20debt%20over%20time%3A%0A%0A%20%20%20%20%24%24d_%7Bt%2B1%7D%3Dp%2B%5Cleft(%5Cfrac%7B1%2Br%7D%7B1%2Bg%7D%5Cright)%20d_%7Bt%7D%20%2B%20%5Cepsilon_%7Bt%2B1%7D%20%5Ctag%7B12%7D%24%24%0A%0A%20%20%20%20The%20loop%20will%20be%20written%20as%20(to%20express%20rates%20in%20terms%20of%20percentage%20points)%3A%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20%20%20%20%20d%5Bt%20%2B%201%5D%20%3D%20p%20%2F%20100%20%2B%20((1%20%2B%20r%20%2F%20100)%20%2F%20(1%20%2B%20g%20%2F%20100))%20*%20d%5Bt%5D%20%2B%20epsilon%5Bt%20%2B%201%5D%20%2F%20100%0A%20%20%20%20%60%60%60%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(d1_slider%2C%20g_slider%2C%20np%2C%20p_slider%2C%20r_slider)%3A%0A%20%20%20%20N_debt_n%20%3D%20500%0A%20%20%20%20p_val_n%20%20%3D%20p_slider.value%0A%20%20%20%20r_val_n%20%20%3D%20r_slider.value%0A%20%20%20%20g_val_n%20%20%3D%20g_slider.value%0A%20%20%20%20d1_val_n%20%3D%20d1_slider.value%0A%0A%20%20%20%20np.random.seed(42)%0A%20%20%20%20epsilon_debt%20%3D%20np.random.randn(N_debt_n)%0A%0A%20%20%20%20d_debt_noise%20%3D%20np.zeros(N_debt_n)%0A%20%20%20%20d_debt_noise%5B0%5D%20%3D%20d1_val_n%0A%0A%20%20%20%20for%20_t%20in%20range(N_debt_n%20-%201)%3A%0A%20%20%20%20%20%20%20%20d_debt_noise%5B_t%20%2B%201%5D%20%3D%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20p_val_n%20%2F%20100%0A%20%20%20%20%20%20%20%20%20%20%20%20%2B%20((1%20%2B%20r_val_n%20%2F%20100)%20%2F%20(1%20%2B%20g_val_n%20%2F%20100))%20*%20d_debt_noise%5B_t%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%2B%20epsilon_debt%5B_t%20%2B%201%5D%20%2F%20100%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20return%20(d_debt_noise%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(d_debt_noise%2C%20go)%3A%0A%20%20%20%20fig_debt_n%20%3D%20go.Figure(%0A%20%20%20%20%20%20%20%20data%3D%5Bgo.Scatter(y%3Dd_debt_noise%2C%20mode%3D%22lines%22%2C%20line%3Ddict(width%3D1.5))%5D%0A%20%20%20%20)%0A%20%20%20%20fig_debt_n.update_layout(%0A%20%20%20%20%20%20%20%20height%3D450%2C%0A%20%20%20%20%20%20%20%20title_text%3D%22The%20evolution%20of%20Public%20Debt%20as%20%25%20of%20GDP%20with%20Noise%22%2C%0A%20%20%20%20%20%20%20%20title_x%3D0.5%2C%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D70%2C%20r%3D60%2C%20t%3D70%2C%20b%3D60)%0A%20%20%20%20)%0A%20%20%20%20fig_debt_n%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20---%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%203.%20Higher%20dimension%20dynamic%20models%0A%20%20%20%20%3Cbr%3E%0A%0A%20%20%20%20The%20stability%20of%20a%20dynamic%20system%20depends%20on%20the%20**eigenvalues**%20of%20the%20characteristic%20matrix%20of%20the%20system.%0A%20%20%20%20In%20our%20simple%20AR(1)%20model%0A%0A%20%20%20%20%24%24y_%7Bt%2B1%7D%3Da%2B%5Crho%20%5Ccdot%20y_%7Bt%7D%24%24%0A%0A%20%20%20%20there%20is%20only%20one%20eigenvalue%2C%20which%20corresponds%20to%20parameter%20%24%5Crho%24.%20%20As%20the%20example%20above%20shows%2C%20if%0A%20%20%20%20%24%7C%5Crho%7C%3C1%24%20the%20process%20has%20a%20unique%20and%20stable%20equilibrium.%20%20On%20the%20other%20hand%2C%20if%20%24%7C%5Crho%7C%3E1%24%20the%20process%0A%20%20%20%20will%20have%20an%20equilibrium%2C%20but%20the%20former%20will%20be%20unstable.%20%20Finally%2C%20if%20%24%7C%5Crho%7C%3D1%24%20there%20will%20be%20no%0A%20%20%20%20equilibrium%20in%20this%20process.%0A%0A%20%20%20%20A%20similar%20reasoning%20can%20be%20applied%20to%20our%20rather%20more%20general%20model%3A%0A%0A%20%20%20%20%24%24Ax_%7Bt%2B1%7D%3D%20Z%20%2B%20Bx_%7Bt%7D%2B%20C%5Cvarepsilon_%7Bt%7D%20%5Ctag%7B13%7D%24%24%0A%0A%20%20%20%20where%20%24A%2C%20B%20%2C%20C%24%20are%20%24n%5Ctimes%20n%24%20matrices%2C%20and%20%24x_%7Bt%2B1%7D%2C%20x_%7Bt%7D%2C%5Cvarepsilon_%7Bt%7D%2C%20Z%24%20are%20%24n%5Ctimes%201%24%20vectors.%0A%20%20%20%20Notice%20also%20that%20in%20order%20to%20simulate%20the%20model%20we%20have%20to%20apply%3A%0A%0A%20%20%20%20%24A%5E%7B-1%7DB%3DD%20%5C%20%2C%20%5C%20A%5E%7B-1%7DC%3DE%20%5C%20%2C%20%5C%20A%5E%7B-1%7DZ%20%3D%20H.%24%0A%0A%20%20%20%20The%20point%20is%20that%20now%20we%20will%20**not%20have**%20just%20one%20eigenvalue.%20%20The%20number%20of%20eigenvalues%20will%20be%20dependent%0A%20%20%20%20on%20the%20number%20of%20variables%20in%20the%20system%20(or%20the%20number%20of%20equations).%20%20For%20example%2C%20if%20our%20system%20has%203%0A%20%20%20%20variables%20(and%20consequently%2C%203%20equations)%20matrix%20%24D%24%20will%20have%20a%20dimension%20of%203%C3%973%20and%20there%20will%20be%20three%0A%20%20%20%20eigenvalues%20(%24%5Clambda_i%2C%20i%3D1%2C2%2C3%24).%20%20If%20all%20%24%7C%5Clambda_i%7C%20%3C1%24%2C%20the%20system%20will%20converge%20to%20a%20stable%0A%20%20%20%20equilibrium%3B%20if%20(at%20least)%20one%20of%20them%20in%20modulus%20is%20larger%20than%20%241%24%20the%20system%20has%20an%20unstable%20equilibrium%3B%0A%20%20%20%20and%20no%20equilibrium%20if%20one%20of%20them%20is%20equal%20to%20%241%24%20in%20modulus.%0A%0A%20%20%20%20See%20next%20figure%20for%20a%20summary%20of%20the%20stability%20conditions%20of%20a%20dynamic%20process%20of%20**dimension%202**%20(so%20we%20have%0A%20%20%20%20two%20eigenvalues).%20%20For%20the%20case%20where%20we%20have%20three%20eigenvalues%2C%20the%20reasoning%20is%20similar.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.Html(%0A%20%20%20%20%20%20%20%20'%3Cimg%20src%3D%22https%3A%2F%2Febs.de.iscte.pt%2Fstability.png%22%20width%3D%22600%22%20style%3D%22max-width%3A100%25%3B%22%3E'%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20A%20Vector%20Auto-Regressive%20model%20of%20order%203%3A%20a%20VAR(3)%20model%0A%0A%20%20%20%20Consider%20the%20following%20model%0A%0A%20%20%20%20%24%24x_%7Bt%2B1%7D%3DD%20x_%7Bt%7D%2BE%20%5Cvarepsilon_%7Bt%7D%20%5Ctag%7B14%7D%24%24%0A%0A%20%20%20%20where%0A%0A%20%20%20%20%24%24x_%7Bt%2B1%7D%20%3D%0A%20%20%20%20%20%20%5Cleft%5B%20%7B%5Cbegin%7Barray%7D%7Bc%7D%0A%20%20%20%20%20%20%20z_%7Bt%2B1%7D%5C%5C%0A%20%20%20%20%20%20%20y_%7Bt%2B1%7D%5C%5C%0A%20%20%20%20%20%20%20v_%7Bt%2B1%7D%0A%20%20%20%20%20%20%20%5Cend%7Barray%7D%20%7D%20%5Cright%5D%20%5Ctag%7B15%7D%24%24%0A%0A%20%20%20%20and%20matrices%20%24D%24%20and%20%24E%24%20are%20given%20by%3A%0A%0A%0A%20%20%20%20%24%24D%20%3D%20%20%5Cleft%5B%20%7B%5Cbegin%7Barray%7D%7Bccc%7D%0A%20%20%20%20%20%20%200.97%20%26%200.10%20%26%20-0.05%20%5C%5C%0A%20%20%20%20%20%20%20-0.3%20%26%200.98%20%26%200.05%20%5C%5C%0A%20%20%20%20%20%20%200.01%20%26-0.04%20%26%200.96%0A%20%20%20%20%20%20%5Cend%7Barray%7D%20%7D%20%5Cright%5D%20%5Cqquad%20%2C%20%5Cqquad%0A%20%20%20%20E%20%3D%0A%20%20%20%20%20%20%5Cleft%5B%20%7B%5Cbegin%7Barray%7D%7Bccc%7D%0A%20%20%20%20%20%20%200%20%26%200%20%26%200%20%5C%5C%0A%20%20%20%20%20%20%200%20%26%200%20%26%200%20%5C%5C%0A%20%20%20%20%20%20%200%20%260%20%26%200%0A%20%20%20%20%20%20%5Cend%7Barray%7D%20%7D%20%5Cright%5D%20%5Ctag%7B16%7D%24%24%0A%0A%0A%20%20%20%20Finally%2C%20please%20assume%20that%20the%20initial%20state%20of%20our%20system%20(or%20its%20initial%20conditions)%20are%3A%20%24z_1%3D1%24%2C%20%24y_1%3D0%24%2C%20and%20%24v_1%3D-1%24.%20For%20clarity%2C%20we%20can%20write%20these%20conditions%20using%20a%20row%20vector%3A%20%24x_1%20%3D%20%5B1%2C0%2C-1%5D%24.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%F0%9F%91%89%20Let%20us%20simulate%20our%20system%20for%20200%20iterations%20to%20glimpse%20its%20dynamics.%20%20Firstly%2C%20we%20have%20to%20pass%20the%0A%20%20%20%20entries%20of%20matrix%20%24D%24%20into%20the%20notebook.%20%20Secondly%2C%20we%20have%20to%20write%20the%20%60for%60%20loop%2C%20and%20finally%2C%20we%20will%0A%20%20%20%20get%20the%20plot.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Matrix%20%24D%24%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np)%3A%0A%20%20%20%20D%20%3D%20np.array(%5B%0A%20%20%20%20%20%20%20%20%5B%200.97%2C%20%200.10%2C%20-0.05%5D%2C%0A%20%20%20%20%20%20%20%20%5B-0.30%2C%20%200.98%2C%20%200.05%5D%2C%0A%20%20%20%20%20%20%20%20%5B%200.01%2C%20-0.04%2C%20%200.96%5D%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%20(D%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20for%20loop%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(D%2C%20np)%3A%0A%20%20%20%20N_var%20%3D%20200%0A%20%20%20%20x1_var%20%3D%20np.array(%5B1%2C%200%2C%20-1%5D%2C%20dtype%3Dfloat)%0A%20%20%20%20x_var%20%3D%20np.zeros((3%2C%20N_var))%0A%20%20%20%20x_var%5B%3A%2C%200%5D%20%3D%20x1_var%0A%0A%20%20%20%20for%20_t%20in%20range(N_var%20-%201)%3A%0A%20%20%20%20%20%20%20%20x_var%5B%3A%2C%20_t%20%2B%201%5D%20%3D%20D%20%40%20x_var%5B%3A%2C%20_t%5D%0A%20%20%20%20return%20(x_var%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20plot%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(go%2C%20x_var)%3A%0A%20%20%20%20fig_var%20%3D%20go.Figure()%0A%20%20%20%20labels%20%20%3D%20%5B%22z(t)%22%2C%20%22y(t)%22%2C%20%22v(t)%22%5D%0A%20%20%20%20colors%20%20%3D%20%5B%22royalblue%22%2C%20%22maroon%22%2C%20%22red%22%5D%0A%0A%20%20%20%20for%20i%2C%20(label%2C%20color)%20in%20enumerate(zip(labels%2C%20colors))%3A%0A%20%20%20%20%20%20%20%20fig_var.add_trace(%0A%20%20%20%20%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20y%3Dx_var%5Bi%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20name%3Dlabel%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3Dcolor)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20fig_var.update_layout(%0A%20%20%20%20%20%20%20%20hovermode%3D%22x%22%2C%0A%20%20%20%20%20%20%20%20title_text%3D%22A%20VAR(3)%20Model%22%2C%0A%20%20%20%20%20%20%20%20title_x%3D0.5%2C%0A%20%20%20%20%20%20%20%20height%3D450%2C%0A%20%20%20%20%20%20%20%20xaxis_title%3D%22time%22%2C%0A%20%20%20%20%20%20%20%20yaxis_title%3D%22z(t)%20%2C%20y(t)%20%2C%20v(t)%22%2C%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D70%2C%20r%3D60%2C%20t%3D70%2C%20b%3D60)%0A%20%20%20%20)%0A%20%20%20%20fig_var%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%F0%9F%91%89%20Checking%20stability%3A%20eigenvalues.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(D%2C%20eigvals)%3A%0A%20%20%20%20eigs_D%20%3D%20eigvals(D)%0A%20%20%20%20eigs_D%0A%20%20%20%20return%20(eigs_D%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Checking%20whether%20the%20condition%20%24%5Csqrt%7B%5Calpha%5E2%2B%5Cbeta%5E2%7D%24%20is%20greater%20than%201%2C%20less%20than%201%2C%20or%20equal%20to%201%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20math%0A%20%20%20%20Cond_D%20%3D%20math.sqrt(0.982782**2%20%2B%200.181356**2)%0A%20%20%20%20Cond_D%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20An%20easier%20way%20is%20to%20use%20the%20%60abs%60%20function%20in%20Python%20(via%20NumPy).%20%20It%20immediately%20gives%20the%20results%20we%20are%0A%20%20%20%20looking%20for%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(eigs_D%2C%20np)%3A%0A%20%20%20%20abs_eigs_D%20%3D%20np.abs(eigs_D)%0A%20%20%20%20abs_eigs_D%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Therefore%2C%20as%20%24%7C%5Csqrt%7B0.944435%5E2-0%5E2%7D%7C%3C1%24%2C%20and%20%24%7C0.999375%7C%3C1%24%2C%20this%20VAR(3)%20model%20is%20stable.%20%20However%2C%20its%0A%20%20%20%20kind%20of%20stability%20is%20a%20very%20special%20one%3A%20it%20takes%20a%20huge%20amount%20of%20time%20to%20converge%20to%20its%20steady-state.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%F0%9F%91%89%20Finally%2C%20simulate%20the%20same%20model%20with%20one%20single%20change%3A%20instead%20of%20having%20%24D_%7B1%2C1%7D%3D0.97%24%2C%20change%20it%20to%0A%20%20%20%20%24D_%7B1%2C1%7D%3D0.99%24.%20%20Check%20the%20eigenvalues%20with%20this%20new%20parameter%20value.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%204%3A%20a%20model%20of%20dimension%202%0A%0A%20%20%20%20Let's%20see%20what%20happens%20in%20the%20following%20model%20with%20dimension%202%2C%20and%20with%20a%20vector%20including%20constants%20%24(Z)%24%3A%0A%0A%20%20%20%20%24%24Y_%7Bt%2B1%7D%3D%20Z%2B%20B%20%5Ccdot%20Y_%7Bt%7D%20%5Ctag%7B17%7D%24%24%0A%0A%20%20%20%20%24%24B%3D%5Cleft%5B%20%7B%5Cbegin%7Barray%7D%7Bcc%7D%0A%20%20%20%20%20%20%201.1%20%26%20-0.4%20%20%5C%5C%0A%20%20%20%20%20%20%200.5%20%26%200.2%20%20%5C%5C%0A%20%20%20%20%20%20%20%5Cend%7Barray%7D%20%7D%20%5Cright%5D%20%5Ctag%7B18%7D%24%24%0A%0A%20%20%20%20%24%24Z%20%3D%20%5Cleft%5B%20%7B%5Cbegin%7Barray%7D%7Bc%7D%0A%20%20%20%20%20%20%2010%20%20%5C%5C%0A%20%20%20%20%20%20%20-25%20%5C%5C%0A%20%20%20%20%20%20%20%5Cend%7Barray%7D%20%7D%20%5Cright%5D%20%5Ctag%7B19%7D%24%24%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%F0%9F%91%89%20a)%20Pass%20the%20entries%20of%20the%20matrices%20%24B%2C%20Z%24%20into%20the%20notebook.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np)%3A%0A%20%20%20%20B%20%3D%20np.array(%5B%5B1.1%2C%20-0.4%5D%2C%20%5B0.5%2C%200.2%5D%5D)%0A%20%20%20%20return%20(B%2C)%0A%0A%0A%40app.cell%0Adef%20_(np)%3A%0A%20%20%20%20Z%20%3D%20np.array(%5B10%2C%20-25%5D%2C%20dtype%3Dfloat)%0A%20%20%20%20return%20(Z%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%F0%9F%91%89%20**b)**%20Calculate%20the%20steady%20state%20of%20this%20model.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(B%2C%20Z%2C%20inv%2C%20np)%3A%0A%20%20%20%20I2%20%3D%20np.eye(len(B))%0A%20%20%20%20Y_bar%20%3D%20inv(I2%20-%20B)%20%40%20Z%0A%20%20%20%20Y_bar%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%3E%20**Answer%20(b)**%0A%20%20%20%20%3E%0A%20%20%20%20%3E%20Here%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%F0%9F%91%89%20**c)**%20Check%20the%20stability%20of%20this%20model.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(B%2C%20eigvals)%3A%0A%20%20%20%20eigs_B%20%3D%20eigvals(B)%0A%20%20%20%20eigs_B%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%3E%20**Answer%20(c)**%0A%20%20%20%20%3E%0A%20%20%20%20%3E%20Here%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%F0%9F%91%89%20**d)**%20Simulate%20the%20model%20for%20a%20period%20of%2030%20iterations.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(B%2C%20Z%2C%20np)%3A%0A%20%20%20%20iterations_2d%20%3D%2030%0A%20%20%20%20Y1_2d%20%3D%20np.array(%5B50%2C%20160%5D%2C%20dtype%3Dfloat)%0A%20%20%20%20Y_2d%20%20%3D%20np.zeros((2%2C%20iterations_2d))%0A%20%20%20%20Y_2d%5B%3A%2C%200%5D%20%3D%20Y1_2d%0A%0A%20%20%20%20for%20_t%20in%20range(iterations_2d%20-%201)%3A%0A%20%20%20%20%20%20%20%20Y_2d%5B%3A%2C%20_t%20%2B%201%5D%20%3D%20Z%20%2B%20B%20%40%20Y_2d%5B%3A%2C%20_t%5D%0A%20%20%20%20return%20(Y_2d%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(Y_2d%2C%20go)%3A%0A%20%20%20%20fig_2d%20%3D%20go.Figure()%0A%0A%20%20%20%20fig_2d.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3DY_2d%5B0%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22y1(t)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22markers%2Blines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(symbol%3D%22circle%22%2C%20size%3D6%2C%20color%3D%22blue%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(width%3D0.3)%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%20%20%20%20fig_2d.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3DY_2d%5B1%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22y2(t)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22markers%2Blines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(symbol%3D%22circle%22%2C%20size%3D6%2C%20color%3D%22red%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(width%3D0.3)%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%0A%20%20%20%20fig_2d.update_layout(%0A%20%20%20%20%20%20%20%20hovermode%3D%22x%22%2C%0A%20%20%20%20%20%20%20%20title_text%3D%22A%202-Dimensional%20System%22%2C%0A%20%20%20%20%20%20%20%20title_x%3D0.5%2C%0A%20%20%20%20%20%20%20%20height%3D450%2C%0A%20%20%20%20%20%20%20%20xaxis_title%3D%22time%22%2C%0A%20%20%20%20%20%20%20%20yaxis_title%3D%22y1(t)%20%2C%20y2(t)%22%2C%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D70%2C%20r%3D60%2C%20t%3D70%2C%20b%3D60)%0A%20%20%20%20)%0A%20%20%20%20fig_2d%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Functions%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(pd)%3A%0A%20%20%20%20%23%20Making%20data%20frames%20looking%20like%20in%20Julia%20and%20Pluto%20(first%205%20rows%20%2B%20last%20row)%0A%20%20%20%20def%20preview(df%2C%20n%3D5)%3A%0A%20%20%20%20%20%20%20%20if%20len(df)%20%3C%3D%20n%20%2B%201%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20df%0A%0A%20%20%20%20%20%20%20%20ellipsis%20%3D%20pd.DataFrame(%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B%5B%22...%22%5D%20*%20df.shape%5B1%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20columns%3Ddf.columns%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20index%3D%5B%22%E2%80%A6%22%5D%2C%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20%20%20%20%20return%20pd.concat(%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20df.head(n)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20ellipsis%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20df.tail(1)%2C%0A%20%20%20%20%20%20%20%20%5D)%0A%20%20%20%20return%20(preview%2C)%0A%0A%0A%40app.cell%0Adef%20_(np)%3A%0A%20%20%20%20def%20crosscor(x%2C%20y%2C%20lags)%3A%0A%20%20%20%20%20%20%20%20%22%22%22Cross-correlation%20at%20given%20lags%2C%20matching%20Julia%20StatsBase.crosscor%20convention.%22%22%22%0A%20%20%20%20%20%20%20%20n%20%3D%20len(x)%0A%20%20%20%20%20%20%20%20x_demean%20%3D%20x%20-%20np.mean(x)%0A%20%20%20%20%20%20%20%20y_demean%20%3D%20y%20-%20np.mean(y)%0A%20%20%20%20%20%20%20%20denom%20%3D%20np.sqrt(np.sum(x_demean**2)%20*%20np.sum(y_demean**2))%0A%20%20%20%20%20%20%20%20result%20%3D%20np.empty(len(lags))%0A%20%20%20%20%20%20%20%20for%20i%2C%20k%20in%20enumerate(lags)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20k%20%3E%3D%200%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20result%5Bi%5D%20%3D%20np.sum(x_demean%5Bk%3A%5D%20*%20y_demean%5B%3An%20-%20k%5D)%20%2F%20denom%20if%20k%20%3C%20n%20else%200.0%0A%20%20%20%20%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20result%5Bi%5D%20%3D%20np.sum(x_demean%5B%3An%20%2B%20k%5D%20*%20y_demean%5B-k%3A%5D)%20%2F%20denom%20if%20-k%20%3C%20n%20else%200.0%0A%20%20%20%20%20%20%20%20return%20result%0A%20%20%20%20return%20(crosscor%2C)%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20%23%20Global%20CSS%20for%20kbd%20elements%20throughout%20the%20notebook%0A%20%20%20%20mo.Html(%22%22%22%0A%20%20%20%20%20%20%20%20%3Cstyle%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20kbd%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20background-color%3A%20%23505050%20!important%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20color%3A%20%23fff%20!important%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20padding%3A%203px%206px%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20border-radius%3A%204px%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20font-family%3A%20monospace%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20font-size%3A%200.9em%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20border%3A%200px%20solid%20%23666%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%3C%2Fstyle%3E%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23background-color%3A%20%23546ccc%20!important%3B%20%23%20A%20color%20that%20looks%20goog%20on%20white%20theme%3B%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
721ab11346db0c56ee1096e0aa81bd1c